/* $Id: PreferencesUnitTest.java 56 2010-01-23 14:49:04Z aaron.day $ */
package org.crazydays.gameplan;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.FileConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.configuration.reloading.ReloadingStrategy;

import org.junit.*;
import static org.junit.Assert.*;

import static org.easymock.EasyMock.*;
import static org.easymock.classextension.EasyMock.*;

import org.crazydays.junit.TestHelper;

/**
 * PreferencesUnitTest
 */
public class PreferencesUnitTest
{
    /** test helper */
    protected TestHelper testHelper = new TestHelper(PreferencesUnitTest.class);

    /**
     * Setup test case user properties.
     */
    @Before
    public void setUp()
    {
        // copy user properties
        File userProperties =
            new File(testHelper.getSandbox(),
                '.' + Preferences.PROPERTIES_FILENAME);

        testHelper.copy(userProperties, "ApplicationExample.properties");

        System.setProperty("user.home", testHelper.getSandbox()
            .getAbsolutePath());
    }

    /**
     * Cleanup test helper.
     */
    @After
    public void tearDown()
    {
        testHelper.tearDown();
    }

    /**
     * Test initialize().
     */
    @Test
    public void testGetInstance()
    {

        // test
        Preferences preferences = new Preferences();

        // verify
        assertEquals("gameplan.testcase.property", "Property", preferences
            .getString("gameplan.testcase.property"));
    }

    /**
     * Test setProperty(String,Object).
     */
    @Test
    public void testSetProperty()
    {
        String key = "preferences.test.property";
        System.setProperty(key, "value");

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        assertTrue("system.contains", preferences.getConfiguration(0)
            .containsKey(key));
        assertFalse("user.contains", preferences.getConfiguration(2)
            .containsKey(key));

        // test
        preferences.setProperty(key, "newvalue");

        // verify
        assertFalse("system.contains", preferences.getConfiguration(0)
            .containsKey(key));
        assertTrue("user.contains", preferences.getConfiguration(2)
            .containsKey(key));
    }

    /**
     * Test setProperty(String,Object).
     */
    @Test(expected = IllegalStateException.class)
    public void testSetProperty_null()
    {
        // objects
        String key = "foo";

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.setProperty(key, "newvalue");
    }

    /**
     * Test setReloadingStrategy(ReloadingStrategy).
     */
    @Test
    public void testSetReloadingStrategy()
    {
        // objects
        ReloadingStrategy strategy = new FileChangedReloadingStrategy();

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();

        // test
        preferences.setReloadingStrategy(strategy);

        // verify
        assertEquals("reloadingStrategy", strategy,
            preferences.userConfiguration.getReloadingStrategy());
    }

    /**
     * Test setReloadingStrategy(ReloadingStrategy).
     */
    @Test(expected = IllegalStateException.class)
    public void testSetReloadingStrategy_null()
    {
        // objects
        ReloadingStrategy strategy = new FileChangedReloadingStrategy();

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.setReloadingStrategy(strategy);
    }

    /**
     * Test getReloadingStrategy().
     */
    @Test
    public void testGetReloadingStrategy()
    {
        // objects
        ReloadingStrategy strategy = new FileChangedReloadingStrategy();

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration.setReloadingStrategy(strategy);

        // test
        ReloadingStrategy result = preferences.getReloadingStrategy();

        // verify
        assertEquals("reloadingStrategy", strategy, result);
    }

    /**
     * Test getReloadingStrategy().
     */
    @Test(expected = IllegalStateException.class)
    public void testGetReloadingStrategy_null()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.getReloadingStrategy();
    }

    /**
     * Test setAutoSave(boolean).
     */
    @Test
    public void testSetAutoSave()
    {
        // objects
        boolean autoSave = true;

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();

        // test
        preferences.setAutoSave(autoSave);

        // verify
        assertEquals("userConfiguration.autoSave", true,
            preferences.userConfiguration.isAutoSave());
    }

    /**
     * Test setAutoSave(boolean).
     */
    @Test(expected = IllegalStateException.class)
    public void testSetAutoSave_null()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.setAutoSave(true);
    }

    /**
     * Test isAutoSave().
     */
    @Test
    public void testIsAutoSave()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration.setAutoSave(true);

        // test
        boolean result = preferences.isAutoSave();

        // verify
        assertEquals("autoSave", true, result);
    }

    /**
     * Test isAutoSave().
     */
    @Test(expected = IllegalStateException.class)
    public void testIsAutoSave_null()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.isAutoSave();
    }

    /**
     * Test setEncoding(String).
     */
    @Test
    public void testSetEncoding()
    {
        // objects
        String encoding = "encoding";

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();

        // test
        preferences.setEncoding(encoding);

        // verify
        assertEquals("userConfiguration.encoding", encoding,
            preferences.userConfiguration.getEncoding());
    }

    /**
     * Test setEncoding(String).
     */
    @Test(expected = IllegalStateException.class)
    public void testSetEncoding_null()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.setEncoding("encoding");
    }

    /**
     * Test getEncoding().
     */
    @Test
    public void testGetEncoding()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration.setEncoding("encoding");

        // test
        String result = preferences.getEncoding();

        // verify
        assertEquals("result", "encoding", result);
    }

    /**
     * Test getEncoding().
     */
    @Test(expected = IllegalStateException.class)
    public void testGetEncoding_null()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.getEncoding();
    }

    /**
     * Test aetBasePath(String).
     */
    @Test
    public void testSetBasePath()
    {
        // objects
        String value = "value";

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();

        // test
        preferences.setBasePath(value);

        // verify
        assertEquals("userConfiguration.basePath", value,
            preferences.userConfiguration.getBasePath());
    }

    /**
     * Test setBasePath(String).
     */
    @Test(expected = IllegalStateException.class)
    public void testSetBasePath_null()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.setBasePath("value");
    }

    /**
     * Test getBasePath().
     */
    @Test
    public void testGetBasePath()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration.setBasePath("value");

        // test
        String result = preferences.getBasePath();

        // verify
        assertEquals("result", "value", result);
    }

    /**
     * Test getBasePath().
     */
    @Test(expected = IllegalStateException.class)
    public void testGetBasePath_null()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.getBasePath();
    }

    /**
     * Test setFileName(String).
     */
    @Test
    public void testSetFileName()
    {
        // objects
        String value = "value";

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();

        // test
        preferences.setFileName(value);

        // verify
        assertEquals("userConfiguration.fileName", value,
            preferences.userConfiguration.getFileName());
    }

    /**
     * Test setFileName(String).
     */
    @Test(expected = IllegalStateException.class)
    public void testSetFileName_null()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.setFileName("value");
    }

    /**
     * Test getFileName().
     */
    @Test
    public void testGetFileName()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration.setFileName("value");

        // test
        String result = preferences.getFileName();

        // verify
        assertEquals("result", "value", result);
    }

    /**
     * Test getFileName().
     */
    @Test(expected = IllegalStateException.class)
    public void testGetFileName_null()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.getFileName();
    }

    /**
     * Test setFile(File).
     */
    @Test
    public void testSetFile()
    {
        // objects
        File value = new File(".");

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();

        // test
        preferences.setFile(value);

        // verify
        assertEquals("userConfiguration.file", value,
            preferences.userConfiguration.getFile());
    }

    /**
     * Test setFile(File).
     */
    @Test(expected = IllegalStateException.class)
    public void testSetFile_null()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.setFile(new File("."));
    }

    /**
     * Test getFile().
     */
    @Test
    public void testGetFile()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration.setFile(new File("."));

        // test
        File result = preferences.getFile();

        // verify
        assertEquals("result", new File("."), result);
    }

    /**
     * Test getFile().
     */
    @Test(expected = IllegalStateException.class)
    public void testGetFile_null()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.getFile();
    }

    /**
     * Test setURL(URL).
     * 
     * @throws MalformedURLException
     */
    @Test
    public void testSetURL()
        throws MalformedURLException
    {
        // objects
        URL value = new URL("http://example.com");

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();

        // test
        preferences.setURL(value);

        // verify
        assertEquals("userConfiguration.file", value.toString(),
            preferences.userConfiguration.getURL().toString());
    }

    /**
     * Test setURL(URL).
     * 
     * @throws MalformedURLException
     */
    @Test(expected = IllegalStateException.class)
    public void testSetURL_null()
        throws MalformedURLException
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.setURL(new URL("http://example.com"));
    }

    /**
     * Test getURL().
     * 
     * @throws MalformedURLException
     */
    @Test
    public void testGetURL()
        throws MalformedURLException
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration.setURL(new URL("http://example.com"));

        // test
        URL result = preferences.getURL();

        // verify
        assertEquals("result", new URL("http://example.com").toString(), result
            .toString());
    }

    /**
     * Test getURL().
     */
    @Test(expected = IllegalStateException.class)
    public void testGetURL_null()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.getURL();
    }

    /**
     * Test load().
     * 
     * @throws ConfigurationException
     */
    @Test
    public void testLoad()
        throws ConfigurationException
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.clear();
        assertFalse("gameplan.testcase.property", preferences
            .containsKey("gameplan.testcase.property"));

        // test
        preferences.load();

        // verify
        assertTrue("gameplan.testcase.property", preferences
            .containsKey("gameplan.testcase.property"));
    }

    /**
     * Test reload().
     */
    @Test
    public void testReload()
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.clear();
        assertFalse("gameplan.testcase.property", preferences
            .containsKey("gameplan.testcase.property"));

        // test
        preferences.reload();

        // verify
        assertTrue("gameplan.testcase.property", preferences
            .containsKey("gameplan.testcase.property"));
    }

    /**
     * Test save().
     * 
     * @throws ConfigurationException
     */
    @Test
    public void testSave()
        throws ConfigurationException
    {
        // mocks
        FileConfiguration configuration = createMock(FileConfiguration.class);

        // expect
        configuration.save();

        // replay
        replay(configuration);

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = configuration;

        // test
        preferences.save();

        // verify
        verify(configuration);
    }

    /**
     * Test save().
     * 
     * @throws ConfigurationException
     */
    @Test(expected = IllegalStateException.class)
    public void testSave_null()
        throws ConfigurationException
    {
        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.save();
    }

    /**
     * Test load(String).
     * 
     * @throws ConfigurationException
     */
    @Test
    public void testLoad_String()
        throws ConfigurationException
    {
        // objects
        String value = "value";

        // mocks
        FileConfiguration configuration = createMock(FileConfiguration.class);

        // expect
        configuration.load(value);

        // replay
        replay(configuration);

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = configuration;

        // test
        preferences.load(value);

        // verify
        verify(configuration);
    }

    /**
     * Test load(String).
     * 
     * @throws ConfigurationException
     */
    @Test(expected = IllegalStateException.class)
    public void testLoad_String_null()
        throws ConfigurationException
    {
        // objects
        String value = null;

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.load(value);
    }

    /**
     * Test save(String).
     * 
     * @throws ConfigurationException
     */
    @Test
    public void testSave_String()
        throws ConfigurationException
    {
        // objects
        String value = "value";

        // mocks
        FileConfiguration configuration = createMock(FileConfiguration.class);

        // expect
        configuration.save(value);

        // replay
        replay(configuration);

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = configuration;

        // test
        preferences.save(value);

        // verify
        verify(configuration);
    }

    /**
     * Test save(String).
     * 
     * @throws ConfigurationException
     */
    @Test(expected = IllegalStateException.class)
    public void testSave_String_null()
        throws ConfigurationException
    {
        // objects
        String value = null;

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.save(value);
    }

    /**
     * Test load(File).
     * 
     * @throws ConfigurationException
     */
    @Test
    public void testLoad_File()
        throws ConfigurationException
    {
        // objects
        File value = new File(".");

        // mocks
        FileConfiguration configuration = createMock(FileConfiguration.class);

        // expect
        configuration.load(value);

        // replay
        replay(configuration);

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = configuration;

        // test
        preferences.load(value);

        // verify
        verify(configuration);
    }

    /**
     * Test load(File).
     * 
     * @throws ConfigurationException
     */
    @Test(expected = IllegalStateException.class)
    public void testLoad_File_null()
        throws ConfigurationException
    {
        // objects
        File value = null;

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.load(value);
    }

    /**
     * Test save(File).
     * 
     * @throws ConfigurationException
     */
    @Test
    public void testSave_File()
        throws ConfigurationException
    {
        // objects
        File value = new File(".");

        // mocks
        FileConfiguration configuration = createMock(FileConfiguration.class);

        // expect
        configuration.save(value);

        // replay
        replay(configuration);

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = configuration;

        // test
        preferences.save(value);

        // verify
        verify(configuration);
    }

    /**
     * Test save(File).
     * 
     * @throws ConfigurationException
     */
    @Test(expected = IllegalStateException.class)
    public void testSave_File_null()
        throws ConfigurationException
    {
        // objects
        File value = null;

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.save(value);
    }

    /**
     * Test load(URL).
     * 
     * @throws ConfigurationException
     * @throws MalformedURLException
     */
    @Test
    public void testLoad_URL()
        throws ConfigurationException, MalformedURLException
    {
        // objects
        URL value = new URL("http://example.com");

        // mocks
        FileConfiguration configuration = createMock(FileConfiguration.class);

        // expect
        configuration.load(value);

        // replay
        replay(configuration);

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = configuration;

        // test
        preferences.load(value);

        // verify
        verify(configuration);
    }

    /**
     * Test load(URL).
     * 
     * @throws ConfigurationException
     */
    @Test(expected = IllegalStateException.class)
    public void testLoad_URL_null()
        throws ConfigurationException
    {
        // objects
        URL value = null;

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.load(value);
    }

    /**
     * Test save(URL).
     * 
     * @throws ConfigurationException
     * @throws MalformedURLException
     */
    @Test
    public void testSave_URL()
        throws ConfigurationException, MalformedURLException
    {
        // objects
        URL value = new URL("http://example.com");

        // mocks
        FileConfiguration configuration = createMock(FileConfiguration.class);

        // expect
        configuration.save(value);

        // replay
        replay(configuration);

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = configuration;

        // test
        preferences.save(value);

        // verify
        verify(configuration);
    }

    /**
     * Test save(URL).
     * 
     * @throws ConfigurationException
     */
    @Test(expected = IllegalStateException.class)
    public void testSave_URL_null()
        throws ConfigurationException
    {
        // objects
        URL value = null;

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.save(value);
    }

    /**
     * Test load(InputStream).
     * 
     * @throws ConfigurationException
     */
    @Test
    public void testLoad_InputStream()
        throws ConfigurationException
    {
        // objects
        InputStream value = new ByteArrayInputStream(new byte[0]);

        // mocks
        FileConfiguration configuration = createMock(FileConfiguration.class);

        // expect
        configuration.load(value);

        // replay
        replay(configuration);

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = configuration;

        // test
        preferences.load(value);

        // verify
        verify(configuration);
    }

    /**
     * Test load(InputStream).
     * 
     * @throws ConfigurationException
     */
    @Test(expected = IllegalStateException.class)
    public void testLoad_InputStream_null()
        throws ConfigurationException
    {
        // objects
        InputStream value = null;

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.load(value);
    }

    /**
     * Test save(InputStream).
     * 
     * @throws ConfigurationException
     */
    @Test
    public void testSave_OutputStream()
        throws ConfigurationException
    {
        // objects
        OutputStream value = new ByteArrayOutputStream();

        // mocks
        FileConfiguration configuration = createMock(FileConfiguration.class);

        // expect
        configuration.save(value);

        // replay
        replay(configuration);

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = configuration;

        // test
        preferences.save(value);

        // verify
        verify(configuration);
    }

    /**
     * Test save(File).
     * 
     * @throws ConfigurationException
     */
    @Test(expected = IllegalStateException.class)
    public void testSave_OutputStream_null()
        throws ConfigurationException
    {
        // objects
        OutputStream value = null;

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.save(value);
    }

    /**
     * Test load(InputStream,String).
     * 
     * @throws ConfigurationException
     */
    @Test
    public void testLoad_InputStream_String()
        throws ConfigurationException
    {
        // objects
        InputStream value = new ByteArrayInputStream(new byte[0]);
        String encoding = "encoding";

        // mocks
        FileConfiguration configuration = createMock(FileConfiguration.class);

        // expect
        configuration.load(value, encoding);

        // replay
        replay(configuration);

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = configuration;

        // test
        preferences.load(value, encoding);

        // verify
        verify(configuration);
    }

    /**
     * Test load(InputStream,String).
     * 
     * @throws ConfigurationException
     */
    @Test(expected = IllegalStateException.class)
    public void testLoad_InputStream_String_null()
        throws ConfigurationException
    {
        // objects
        InputStream value = null;
        String encoding = null;

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.load(value, encoding);
    }

    /**
     * Test save(InputStream,String).
     * 
     * @throws ConfigurationException
     */
    @Test
    public void testSave_OutputStream_encoding()
        throws ConfigurationException
    {
        // objects
        OutputStream value = new ByteArrayOutputStream();
        String encoding = "encoding";

        // mocks
        FileConfiguration configuration = createMock(FileConfiguration.class);

        // expect
        configuration.save(value, encoding);

        // replay
        replay(configuration);

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = configuration;

        // test
        preferences.save(value, encoding);

        // verify
        verify(configuration);
    }

    /**
     * Test save(OutputStream,String).
     * 
     * @throws ConfigurationException
     */
    @Test(expected = IllegalStateException.class)
    public void testSave_OutputStream_String_null()
        throws ConfigurationException
    {
        // objects
        OutputStream value = null;
        String encoding = null;

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.save(value, encoding);
    }

    /**
     * Test load(Reader).
     * 
     * @throws ConfigurationException
     */
    @Test
    public void testLoad_Reader()
        throws ConfigurationException
    {
        // objects
        Reader value = new StringReader("");

        // mocks
        FileConfiguration configuration = createMock(FileConfiguration.class);

        // expect
        configuration.load(value);

        // replay
        replay(configuration);

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = configuration;

        // test
        preferences.load(value);

        // verify
        verify(configuration);
    }

    /**
     * Test load(Reader).
     * 
     * @throws ConfigurationException
     */
    @Test(expected = IllegalStateException.class)
    public void testLoad_Reader_null()
        throws ConfigurationException
    {
        // objects
        Reader value = null;

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.load(value);
    }

    /**
     * Test save(Writer).
     * 
     * @throws ConfigurationException
     */
    @Test
    public void testSave_Writer()
        throws ConfigurationException
    {
        // objects
        Writer value = new StringWriter();

        // mocks
        FileConfiguration configuration = createMock(FileConfiguration.class);

        // expect
        configuration.save(value);

        // replay
        replay(configuration);

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = configuration;

        // test
        preferences.save(value);

        // verify
        verify(configuration);
    }

    /**
     * Test save(Writer).
     * 
     * @throws ConfigurationException
     */
    @Test(expected = IllegalStateException.class)
    public void testSave_Writer_null()
        throws ConfigurationException
    {
        // objects
        Writer value = null;

        // setup
        Preferences preferences = new Preferences();
        preferences.initialize();
        preferences.userConfiguration = null;

        // test
        preferences.save(value);
    }
}
